package com.example.anju.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.example.anju.entity.Orders;

@Mapper
@Repository
public interface OrdersMapper {
	// 通过orders_id查询订单
	@Select("SELECT * FROM view_orders_house where orders_id=#{orders_id}")
	List<Orders> findOrdersByOrdersId(@Param("orders_id") int orders_id);

	// 通过orders_landlord_id查询订单
	@Select("SELECT * FROM view_orders_house where orders_landlord_id=#{orders_landlord_id}")
	List<Orders> findOrdersByLandlordId(@Param("orders_landlord_id") int orders_landlord_id);

	// 通过orders_tenant_id查询订单
	@Select("SELECT * FROM view_orders_house where orders_tenant_id=#{orders_tenant_id}")
	List<Orders> findOrdersByTenantId(@Param("orders_tenant_id") int orders_tenant_id);

	// 通过orders_landlord_id和check_orders查询订单
	@Select("SELECT * FROM view_orders_house where orders_landlord_id=#{orders_landlord_id} AND check_orders=#{check_orders}")
	List<Orders> findOrdersByLandlordIdAndCheck(Orders orders);

	// 通过orders_tenant_id和check_orders查询订单
	@Select("SELECT * FROM view_orders_house where orders_tenant_id=#{orders_tenant_id} AND check_orders=#{check_orders}")
	List<Orders> findOrdersByTenantIdAndCheck(Orders orders);

	// 通过orders_tenant_id和orders_house_id查询订单
	@Select("SELECT * FROM orders where orders_tenant_id=#{orders_tenant_id} AND orders_house_id=#{orders_house_id}")
	List<Orders> findOrdersByTenantIdAndHouseId(Orders orders);

	// 通过orders_house_id和check_orders查询订单
	@Select("SELECT * FROM view_orders_house where orders_house_id=#{orders_house_id} AND check_orders='Y'")
	List<Orders> findOrdersByOrdersHouseAndHouseId(@Param("orders_house_id") int orders_house_id);

	// 通过orders_id和check查询订单
	@Select("SELECT * FROM view_orders_house where orders_id=#{orders_id} AND check_orders=#{check_orders}")
	List<Orders> findOrdersByOrdersIdAndCheck(Orders orders);

	// 生成订单
	@Insert("INSERT INTO orders VALUES(#{orders_id},#{orders_tenant_id},#{orders_landlord_id},#{orders_house_id},#{check_orders},#{check_in},#{check_out},#{contract})")
	@Options(useGeneratedKeys = true, keyProperty = "orders_id", keyColumn = "orders_id")
	void publishOrders(Orders orders);

	// 通过orders_id修改订单状态
	@Insert("UPDATE orders SET check_orders=#{check_orders} WHERE orders_id=#{orders_id}")
	void postOrdersByOrdersId(Orders orders);
}
